Conditionals
Lecture 4
Review
- Functions are used to make our lives easier
- Call function, pass it arguments, get a returned value
- mean(), median(), min(), max(), summary()
- nrow(), ncol(), sum(), print()
- Read R documentation to learn what each function does
- help(mean), ?mean
What are some
Conditionals in English?
Example:
X is 2.
If X is greater than 0, X is positive.
Condition
What happens if
condition is true
Conditionals: if
Syntax:
if(_some condition here_) {
Code here runs
if condition is true
}
Example:
Conditionals: if Notes
Syntax:
if(_some condition here_) {
Code here runs
if condition is true
}
Example:
Good style to have {}
surrounding code inside if
statement
Conditionals: if Notes
Syntax:
if(_some condition here_) {
Code here runs
if condition is true
}
Example:
Good style to indent code inside if
statement
Makes code easier to read
Your Turn: sleep dataset conditional
- If number of students in dataset is less
than 20, print “dataset is too small”
Syntax Hints:
if(_some condition here_) {
Code here runs
if condition is true
}
nrow()
> print(sleep)
extra group ID
1 0.7 1 1
2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10
Sleep Dataset Conditional
- What is our condition?
- nrow(sleep) < 20
- What should we output if condition is true?
- print(“dataset is too small”)
- Combine into if statement
What is the output of this code?
Sometimes it’s confusing if
there is no output
Did the code work properly?
Example:
X is 2.
If X is greater than 0, X is positive.
Else, X is negative
What happens if condition is false
What happens if
condition is true
Can you see the
issue with this
statement?
Conditionals: if & else
Syntax:
if(_some condition here_) {
Code here runs
if condition is true
} else {
Code here runs if
condition is false
}
Example:
Conditionals: if & else Notes
Syntax:
if(_some condition here_) {
Code here runs
if condition is true
} else {
Code here runs if
condition is false
}
Example:
No condition here, runs when condition
from if is false
Your Turn: sleep dataset if & else
- If number of students in
dataset is less than 20,
print “dataset is too
small”
- Else print “this is a large
dataset”
Syntax Hints:
if(_some condition here_) {
Code here runs
if condition is true
} else {
Code here runs if
condition is false
}
nrow()
Sleep Dataset Conditional
What is the output of this code?
Are these equivalent?
Example:
X is 2.
If X is greater than 0, X is positive.
Else, X is negative
What happens if condition is false
What happens if
condition is true
What about a third
case, when X is
zero?
Example:
X is 2.
If X is greater than 0, X is positive
Else if X equals 0, then X is zero
Else, X is negative
What about a third
case, when X is
zero?
Example:
X is 2.
If X is greater than 0, X is positive
Else if X equals 0, then X is zero
Else, X is negative
Happens if second
condition is true
second condition, checked after if is false
Conditionals: if & else if & else
Syntax:
if(_condition A_) {
Code here runs
if condition is true
} else if (_condition B_) {
Code here runs if Condition B true
And Condition A false
} else {
Code here runs if
condition is false
}
Example:
Conditionals: if & else if & else Notes
Example:
Check for equality:
need TWO equal signs!
==
Conditionals: if & else if & else Notes
Example:
This condition only
checked if first
condition x < 0 was
false
We can add as many else if
statements as we want
Your Turn: sleep dataset if & else if & else
Syntax Hints:
if(_condition A_) {
Code here runs
if condition is true
} else if (_condition B_) {
Code here runs if
Condition B true
And Condition A false
} else {
Code here runs if
condition is false
}
nrow()
Condition, if
number of
students is:
Print
Less than 20
“Dataset too small”
20
-29
“Dataset is acceptable size”
30
-39
“Dataset is medium sized”
Greater than or
equal to 40
“Dataset is large”
Your Turn: sleep dataset if & else if & else
Operators
<, >
Less than, greater than
<=, >=
Less than or equal to, greater than or equal to
==
Equal to
!=
Not equal to
Boolean Operators
&
And
|
Or
Conditionals with Vectors
ifelse()
- Very similar to what we just learned
- Used for vectors, condition applied to each element of
vector, and that new vector is returned
Syntax:
ifelse(condition, what happens if true, what happens if false)
ifelse() Example
Syntax:
ifelse(condition, what happens if true, what happens if false)
ifelse() Example
vector1 element
Element < 0?
vector2 result
-
4
True
-
1
-
3
True
-
1
-
2
True
-
1
-
1
True
-
1
0
False
1
1
False
1
2
False
1
Your Turn: sleep dataset ifelse()
- Create vector group that stores
“control” if group is 1
“experimental” if group is 2
Syntax Hint:
ifelse(condition, what
happens if
true, what happens if false)
> print(sleep)
extra group ID
1 0.7 1 1
2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10
Your Turn: sleep dataset ifelse()
- How many people gained sleep in the
dataset?
- Hint: create vector that stores 1 if
person gained sleep (extra >0) and 0 if
did not (extra <=0)
Syntax Hint:
ifelse(condition, what happens if
true, what happens if false)
> print(sleep)
extra group ID
1 0.7 1 1
2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10
Sleep dataset ifelse()
Misc.
- These are equivalent
- Use boolean & or | to specify more specific conditions
Your Turn: sleep dataset ifelse()
- Create vector sleep_diff that stores the
absolute value of all the sleep hour
differences
- Use ifelse() to do this
Syntax Hint:
ifelse(condition, what happens if
true, what happens if false)
> print(sleep)
extra group ID
1 0.7 1 1
2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10
Sleep dataset ifelse()
Your Turn: sleep dataset ifelse()
- Create vector sleep_gained_lost that
stores the following strings based on what
value sleep$extra is
> print(sleep)
extra group ID
1 0.7 1 1
2 -1.6 1 2
3 -0.2 1 3
4 -1.2 1 4
5 -0.1 1 5
6 3.4 1 6
7 3.7 1 7
8 0.8 1 8
9 0.0 1 9
10 2.0 1 10
11 1.9 2 1
12 0.8 2 2
13 1.1 2 3
14 0.1 2 4
15 -0.1 2 5
16 4.4 2 6
17 5.5 2 7
18 1.6 2 8
19 4.6 2 9
20 3.4 2 10
sleep$extra value
Extra > 0
Extra = 0
Extra < 0
Sleep dataset ifelse()
Applications of ifelse() will
become more clear when
we learn about data
cleaning (lecture 6)
Logistics
Assignment 1 due today at midnight
Assignment 2 available after class today
Good faith attempt required
Collaboration with other students encouraged